package io.github.jyrmc.deploy.task

import io.github.jyrmc.deploy.utils.CommandLineUtils
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

/**
 * k8s search deployment
 *
 * @author jy
 * @since 2023/5/16 11:24
 **/
open class SearchPodTask : DefaultTask() {
    @Input
    @Optional
    lateinit var host: String

    @Input
    var port: Int = 22

    @Input
    var username: String = "root"

    @Input
    @Optional
    lateinit var password: String

    @Input
    @Optional
    lateinit var imageName: String

    @Input
    @Optional
    lateinit var deploymentName: String

    @Input
    var namespace: String = "default"


    @TaskAction
    fun getPodStatusAction() {
        if (host.isBlank()) {
            println("⚡⚡⚡⚡⚡⚡host can't be null or empty⚡⚡⚡⚡⚡⚡")
            return
        }
        if (password.isBlank()) {
            println("⚡⚡⚡⚡⚡⚡password can't be null or empty⚡⚡⚡⚡⚡⚡")
            return
        }
        if (imageName.isBlank()) {
            println("⚡⚡⚡⚡⚡⚡imageName can't be null or empty⚡⚡⚡⚡⚡⚡")
            return
        }

        // 获取pod运行状态
        val podStatusCommandLine = "kubectl get pods -n $namespace | grep $deploymentName"
        val result = CommandLineUtils.exec(host, port, username, password, podStatusCommandLine)
        println("========================================================================================================================================\n")
        if (result.isNotBlank()) {
            println("🚩🚩🚩🚩🚩🚩$result🚩🚩🚩🚩🚩🚩")
        }  else {
            println("🚨🚨🚨🚨🚨🚨 Pod Is Not Running Or Application Is Not Deployed🚨🚨🚨🚨🚨🚨")
        }
        println("========================================================================================================================================")
    }
}